<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="../css/bootstrap.min.css">
  <!-- <link rel="stylesheet" href="../css/remove.css"> -->
</head>

<body>
  <div class="container">
    <h1>王者荣耀</h1>

    <a type="button" class="btn btn-success" href="./index.html">添加</a>

    <form>

    </form>

  </div>
  <script src="../js/jquery.min.js"></script>
  <script>
    $(() => {
      const params = new URLSearchParams(location.search);
      let id = params.get('id');
      $.ajax({
        type: 'get',
        url: 'http://127.0.0.1:8080/heors/' + id,
        success(res) {
          const { message, status } = res;
          let html = `
          <legend>编辑英雄</legend>
            <div class="form-group">
            <label for="">name</label>
            <input type="text" class="form-control" id="name" value=${message[0].name}>
          </div>
          <div class="form-group">
            <label for="">age</label>
            <input type="text" class="form-control" id="age" value=${message[0].age}>
          </div>
          <button type="button" class="btn btn-primary" id='editBtn'>确定</button>`;
          $('form').html(html);
        }
      })
      $('form').on('click', '#editBtn', function () {
        $.ajax({
          type: 'put',
          url: 'http://127.0.0.1:8080/heors/' + id,
          data: {
            name: $('#name').val(),
            age: $('#age').val()
          },
          success(res) {
            const { message, status } = res;
            if (status != -200) {
              alert('编辑成功');
            } else {
              alert('编辑失败');
            }
            location.href = './index.html';
          }
        })
      })
    })
  </script>
</body>

</html>